package com.example.controller;

import com.example.constants.MyConstants;
import com.example.pojo.Instruction;
import com.example.resultType.Result;
import com.example.service.InstructionService;
import com.example.service.InstructionServiceImpl;
import com.example.utils.PicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@RestController
@RequestMapping("/pic")
public class PicController {

    @Autowired
    private InstructionServiceImpl service;

    /**
     * 根据指令id找到对应的图片
     * @param id 指令id，
     * @return 返回对应的图片二进制字符串数组
     * @throws IOException
     */
    @RequestMapping("/getPicById/{id}")
    public String getPicById(@PathVariable int id) throws IOException {
        Instruction instruction = service.queryById(id);
        String image = instruction.getImage();

        byte[] bytes = PicUtil.readInputStream(image);
        Result result = new Result();
        result.setData(bytes);
        return result.toString();
    }

}
